package com.zlsy.springbootdemo.rabbitmq.producer;

import com.zlsy.springbootdemo.rabbitmq.stream.Source;
import org.springframework.integration.annotation.InboundChannelAdapter;
import org.springframework.integration.annotation.Poller;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;

import java.util.Random;

/**
 * @author zhouliang
 * @date 2021/1/6 20:41
 */
@Component
public class PartitionProducer {
  private static final Random RANDOM = new Random(System.currentTimeMillis());

  private static final String[] data =
      new String[] {
        "abc1", "def1", "qux1",
        "abc2", "def2", "qux2",
        "abc3", "def3", "qux3",
        "abc4", "def4", "qux4",
      };

  @InboundChannelAdapter(channel = Source.PARTITION_OUTPUT, poller = @Poller(fixedRate = "5000000"))
  public Message<?> generate() {
    String value = data[RANDOM.nextInt(data.length)];
    System.out.println("Sending: " + value);
    return MessageBuilder.withPayload(value).setHeader("partitionKey", value).build();
  }
}
